﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class EditUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserInfo();
            }
        }

        private void BindUserInfo()
        {
            long userID = Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_USER_ID]);
            UserService userService = new UserService();
            User userEntity = userService.GetUserByID(userID);
            if (!userEntity.UserTypeReference.IsLoaded)
            {
                userEntity.UserTypeReference.Load();
            }
            //roleTypeTxtBox.Text = userEntity.UserType.Name;
            RoleDDL.DataSource = userService.GetUserTypeList();
            RoleDDL.DataTextField = "Name";
            RoleDDL.DataValueField = "ID";
            RoleDDL.DataBind();
            RoleDDL.SelectedValue = userEntity.UserType.ID.ToString();
            if (!userEntity.ServiceStationReference.IsLoaded)
            {
                userEntity.ServiceStationReference.Load();
            }
            if (userEntity.ServiceStation != null)
            {
                stationTxtBox.Text = userEntity.ServiceStation.Name;
            }
            customerIDTxtBox.Text = userEntity.UserName.ToString();
            customerNameTxtBox.Text = userEntity.FullName;
            companyNameTxtBox.Text = userEntity.CompanyName;
            contactTxtBox.Text = userEntity.ContactPerson;
            phoneTxtBox.Text = userEntity.UserPhone;
            mobilePhoneTxtBox.Text = userEntity.UserMobilePhone;
            faxTxtBox.Text = userEntity.UserFax;
            emailTxtBox.Text = userEntity.UserEmail;
            addressTxtBox.Text = userEntity.UserAddress;
            commentTxtBox.Text = userEntity.Comment;

            ProductClassService classService = new ProductClassService();
            OEMProductClassDDL.DataSource = classService.GetOEMProdutClassWithSelectOne();
            OEMProductClassDDL.DataTextField = "Name";
            OEMProductClassDDL.DataValueField = "ID";
            OEMProductClassDDL.DataBind();
            if (!userEntity.ProductClassReference.IsLoaded)
            {
                userEntity.ProductClassReference.Load();
            }
            if (userEntity.ProductClass != null)
            {
                OEMProductClassDDL.SelectedValue = userEntity.ProductClass.ID.ToString();
            }
            else
            {
                OEMProductClassDDL.SelectedIndex = 0;
            }
        }
        
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                long userID = Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_USER_ID]);
                UserService userService = new UserService();
                User userEntity = userService.GetUserByID(userID);
                userEntity.FullName = customerNameTxtBox.Text;
                userEntity.CompanyName = companyNameTxtBox.Text;
                userEntity.ContactPerson = contactTxtBox.Text;
                userEntity.UserPhone = phoneTxtBox.Text;
                userEntity.UserMobilePhone = mobilePhoneTxtBox.Text;
                userEntity.UserFax = faxTxtBox.Text;
                userEntity.UserEmail = emailTxtBox.Text;
                userEntity.UserAddress = addressTxtBox.Text;
                userEntity.Comment = commentTxtBox.Text;
                userEntity.UserType = userService.GetUserTypeByID(Convert.ToInt32(RoleDDL.SelectedValue));
                if (!string.IsNullOrEmpty(passwordTextBox.Text))
                {
                    userEntity.UserPassword = passwordTextBox.Text;
                }

                if (OEMProductClassDDL.SelectedValue.Equals("0"))
                {
                    userEntity.ProductClass = null;
                }
                else
                {
                    ProductClassService classService = new ProductClassService();
                    userEntity.ProductClass = classService.GetProductClass(Convert.ToInt32(OEMProductClassDDL.SelectedValue));
                }
                userService.UpdateUser(userEntity);
                ClientScript.RegisterStartupScript(this.GetType(), "Tip", "alert('用户信息已成功更新');", true);
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
                LogUtility.LogUtilityIntance.LogException(ex, "EditUser.aspx.cs -> SaveButton_Click");
            }
        }
    }
}
